package com.bzzoneit.newsys.web.admin;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bzzoneit.newsys.entity.admin.Role;
import com.bzzoneit.newsys.service.admin.RoleService;
import com.bzzoneit.newsys.utils.dwz.AjaxDone;
import com.bzzoneit.newsys.utils.page.Page;

@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {
	
	@Autowired
	private RoleService roleService;
	
	private Role Role;
	
	public Role getRole() {
		return Role;
	}

	public void setRole(Role Role) {
		this.Role = Role;
	}

	private List<Role> list;

	

	public List<Role> getList() {
		return list;
	}

	public void setList(List<Role> list) {
		this.list = list;
	}

	@RequestMapping("/list")
	public String list(
			@RequestParam("navtabid") String navtabid,
			Model model) {
		list=roleService.getRoleList();
		model.addAttribute("list", list);
		model.addAttribute("navtabid", navtabid);
		return "role/list";
	}
	
	@ResponseBody
	@RequestMapping("/delete/{id}")
	public AjaxDone delete(@PathVariable("id") Long id,@RequestParam("navtabid") String navtabid) {
		roleService.deleteRole(id);
		AjaxDone a=new AjaxDone();
		a.setStatusCode(200);
		a.setMessage("删除成功");
		a.setNavTabId(navtabid); //重新加载这个nav
		return a;
	}
	
	@RequestMapping(value="/edit/{id}")
	public String prepare(Model model,@PathVariable("id") Long id,
			@RequestParam("navtabid") String navtabid
		) {
		model.addAttribute("navtabid", navtabid);
		model.addAttribute("role", roleService.getRole(id));
		return "role/edit";
	}
	

	@RequestMapping(value="/add")
	public String add(Model model,@RequestParam("navtabid") String navtabid) {
		model.addAttribute("navtabid", navtabid);
		return "role/add";
	}
	
	@ResponseBody
	@RequestMapping("/insert")
	public AjaxDone insertUser(@ModelAttribute("role")Role role,
			@RequestParam("navtabid") String navtabid) {
		roleService.insertRole(role);
		AjaxDone a=new AjaxDone();
		a.setStatusCode(200);
		a.setCallbackType("closeCurrent");
		a.setMessage("新增成功");
		a.setForwardUrl(""); 
		a.setNavTabId(navtabid); //重新加载这个nav
		return a;
	}
	
	@ResponseBody
	@RequestMapping("/save")
	public AjaxDone update(@ModelAttribute("role")Role role,
			@RequestParam("navtabid") String navtabid
			) 
	{
		roleService.updateRole(role);
		AjaxDone a=new AjaxDone();
		a.setStatusCode(200);
		a.setCallbackType("closeCurrent");
		a.setMessage("更新成功");
		a.setForwardUrl("");
		a.setNavTabId(navtabid); //重新加载这个nav
		return a;
	}
	
	
	@RequestMapping(value="/roleMenuList/{id}")
	public String roleMenuList(Model model,@PathVariable("id") Long id,
			@RequestParam("navtabid") String navtabid
		) {
		model.addAttribute("navtabid", navtabid);
		model.addAttribute("role", roleService.getRole(id));
		return "role/rolemenulist";
	}

}
